//
//  VRSlideMenuViewController.m
//  MyDemos
//
//  Created by Veer on 16/8/23.
//  Copyright © 2016年 veer. All rights reserved.
//

#import "VRSlideMenuViewController.h"

static CGFloat LeftGestureWidth = 45.0;
static CGFloat ContentViewVisibleWidth = 60.0;
static CGFloat AnimateDuration = 0.3;


@interface VRSlideMenuViewController() <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UIView *leftMenuContainer;
@property (strong, nonatomic) UIView *contentContainer;
@property (strong, nonatomic) UIView *gestureRecongnizerView;

@end

@implementation VRSlideMenuViewController


- (instancetype)initWithContentViewController:(UIViewController *)contentViewController leftViewController:(UIViewController *)leftViewController
{
    if (self == [super init]) {
        
        self.contentViewController = contentViewController;
        self.leftMenuViewController = leftViewController;
        [self prepare];
    }
    return self;
}


- (void)prepare
{

    _leftMenuContainer = [[UIView alloc] init];
    _contentContainer = [[UIView alloc] init];
    _gestureRecongnizerView = [[UIView alloc] init];
    _gestureRecongnizerView.hidden = YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.leftMenuContainer.frame = self.view.frame;
    [self.leftMenuContainer addSubview:self.leftMenuViewController.view];
    [self.view addSubview:self.leftMenuContainer];
    [self addChildViewController:self.leftMenuViewController];
    [self.leftMenuViewController didMoveToParentViewController:self];
    
    self.contentContainer.frame = self.view.frame;
    [self.contentContainer addSubview:self.contentViewController.view];
    [self.view addSubview:self.contentContainer];
    [self addChildViewController:self.contentViewController];
    [self.contentViewController didMoveToParentViewController:self];
    
    self.gestureRecongnizerView.frame = self.view.frame;
    [self.contentContainer addSubview:self.gestureRecongnizerView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTapGestureRecongnizer:)];
    [self.gestureRecongnizerView addGestureRecognizer:tap];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPanGestureRecongnizer:)];
    pan.delegate = self;
    [self.contentContainer addGestureRecognizer:pan];
    
}


- (void)showLeftMenu:(BOOL)isShow
{
    
    // 计算动画需要时间
    CGFloat menuVisibleW = self.contentContainer.frame.size.width - ContentViewVisibleWidth;
    __block typeof(self) weakself = self;
    if (isShow) {   // 显示
        
        CGFloat duration = (menuVisibleW - self.contentContainer.x) / menuVisibleW * AnimateDuration;
        [UIView animateWithDuration:duration animations:^{
            
            weakself.contentContainer.x = menuVisibleW;
            weakself.leftMenuContainer.x = 0;
        } completion:^(BOOL finished) {
            
            weakself.gestureRecongnizerView.hidden = NO;
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        }];
    }else{  // 隐藏
        
        
        CGFloat duration = self.contentContainer.x / menuVisibleW * AnimateDuration;
        [UIView animateWithDuration:duration animations:^{
            
            weakself.contentContainer.x = 0;
            weakself.leftMenuContainer.x = -menuVisibleW / 2;
        } completion:^(BOOL finished) {
            
            weakself.gestureRecongnizerView.hidden = YES;
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }];
    }
}


- (void)showViewController:(UIViewController *)viewController
{
    if ([self.contentViewController isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *navVc = (UINavigationController *)self.contentViewController;
        [navVc pushViewController:viewController animated:YES];
        [self showLeftMenu:NO];
    }else{
    
        id<VRSlideMenuViewControllerDelegate> delegate = (id<VRSlideMenuViewControllerDelegate>)self.contentViewController;
        if ([delegate respondsToSelector:@selector(getNavigationController)]) {
            
            UINavigationController *navVc = [delegate getNavigationController];
            [navVc pushViewController:viewController animated:YES];
            [self showLeftMenu:NO];
        }
    }
    [self.navigationController pushViewController:viewController animated:YES];
//    [self presentViewController:viewController animated:YES completion:nil];
}


#pragma mark - 点击事件
- (void)handelTapGestureRecongnizer:(UITapGestureRecognizer *)recongnizer
{
    [self showLeftMenu:NO];
}


- (void)handelPanGestureRecongnizer:(UIPanGestureRecognizer *)recognizer
{
    
    CGPoint point = [recognizer translationInView:self.view];
    CGFloat menuVisibleW = self.contentContainer.frame.size.width - ContentViewVisibleWidth;
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        
        
    }else if (recognizer.state == UIGestureRecognizerStateChanged){
        
        CGRect frame = self.contentContainer.frame;
        CGFloat originX = frame.origin.x + point.x;
        if (originX >= menuVisibleW) {
            frame.origin.x = menuVisibleW;
        }else if (originX < 0){
            frame.origin.x = 0;
        }else{
            frame.origin.x += point.x;
        }
        
        self.contentContainer.frame = frame;
        frame.origin.x = (frame.origin.x - menuVisibleW) / 2;
        self.leftMenuContainer.frame = frame;
        [recognizer setTranslation:CGPointZero inView:self.view];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
    
        BOOL isShow = self.contentContainer.frame.origin.x > menuVisibleW * 0.5;
        [self showLeftMenu:isShow];
    }
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    
    UINavigationController *navVc = nil;
    if ([self.contentViewController isKindOfClass:[UINavigationController class]]) {
        
        navVc = (UINavigationController *)self.contentViewController;
    }else{
        
        id<VRSlideMenuViewControllerDelegate> delegate = (id<VRSlideMenuViewControllerDelegate>)self.contentViewController;
        if ([delegate respondsToSelector:@selector(getNavigationController)]) {
            
            navVc = [delegate getNavigationController];
        }
    }
    
    // 判断有没有超出滑动边界，或者不是栈顶控制器
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (point.x > LeftGestureWidth || navVc.childViewControllers.count != 1) {
        return NO;
    }
    
    return YES;
}


- (BOOL)shouldAutorotate
{
    return [self.contentViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.contentViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.contentViewController preferredInterfaceOrientationForPresentation];
}



@end
